package com.shanjupay.merchant.controller;

import com.shanjupay.common.domain.BusinessException;
import com.shanjupay.common.domain.CommonErrorCode;
import com.shanjupay.common.domain.PageVO;
import com.shanjupay.common.util.QRCodeUtil;
import com.shanjupay.merchant.api.MerchantService;
import com.shanjupay.merchant.api.dto.MerchantDTO;
import com.shanjupay.merchant.api.dto.StoreDTO;
import com.shanjupay.merchant.utils.SecurityUtil;
import com.shanjupay.transaction.api.TransactionService;
import com.shanjupay.transaction.api.dto.QRCodeDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @program: shanjupay
 * @description: 门店管理
 * @author: Mr.Hkc
 * @create: 2020-08-21 08:31
 **/
@Api(value = "商户平台‐门店管理", tags = "商户平台‐门店管理", description = "商户平台‐门店的增删改查")
@RestController
public class StoreController {

    @Reference
    MerchantService merchantService;

    @Reference
    TransactionService transactionService;


    @Value("${shanjupay.c2b.subject}")
    private String subject;

    @Value("${shanjupay.c2b.body}")
    private String body;



    @ApiOperation("删除某门店")
    @ApiImplicitParam(name = "id",value = "门店id",required = true,dataType = "Long",paramType = "path")
    @DeleteMapping("/my/stores/{id}")
    public void removeStore(@PathVariable Long id) {
        merchantService.removeStore(id);
    }


    @ApiOperation("修改某门店信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeDTO",value = "门店信息",required = true,dataType = "StoreDTO",paramType = "body"),
            @ApiImplicitParam(name = "staffIds",value = "管理员id集合",required = true,allowMultiple = true,dataType = "Long",paramType = "query")
    })
    @PutMapping("/my/stores")
    public void modifyStaff(@RequestBody StoreDTO storeDTO,@RequestParam List<Long> staffIds) {
        merchantService.modifyStore(storeDTO,staffIds);
    }


    @ApiOperation("查询某个门店的信息")
    @ApiImplicitParam(name = "id",value = "门店id",dataType = "Long",paramType = "path")
    @GetMapping("/my/stores/{id}")
    public StoreDTO queryStaff(@PathVariable Long id) {
        return merchantService.queryStoreById(id);
    }


    @ApiOperation("生成商店应用门店二维码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId", value = "商店应用id", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "storeId", value = "商户门店id", required = true, dataType = "String", paramType = "path")
    })
    @GetMapping(value = "/my/apps/{appId}/stores/{storeId}/app-store-qrcode")
    public String createCScanBStoreQRCode(@PathVariable(value = "appId") String appId,
                                          @PathVariable(value = "storeId") Long storeId) throws BusinessException {
        //商户id
        Long merchantId = SecurityUtil.getMerchantId();
        //商户信息
        MerchantDTO merchantDTO = merchantService.queryMerchantById(merchantId);
        QRCodeDto qrCodeDto = new QRCodeDto();
        qrCodeDto.setMerchantId(merchantId);
        qrCodeDto.setStoreId(storeId);
        qrCodeDto.setAppId(appId);
        //"%s 商品"
        String subjectFormat = String.format(subject, merchantDTO.getMerchantName());
        qrCodeDto.setSubject(subjectFormat);
        //内容,格式:"向%s付款"
        String bodyFormat = String.format(body, merchantDTO.getMerchantName());
        qrCodeDto.setBody(bodyFormat);
        //获得二维码url
        String storeQRCodeURL = transactionService.createStoreQRCode(qrCodeDto);
        //生成二维码
        QRCodeUtil qrCodeUtil = new QRCodeUtil();
        try {
            //返回二维码base64编码
            return qrCodeUtil.createQRCode(storeQRCodeURL, 200, 200);
        } catch (IOException e) {
            throw new BusinessException(CommonErrorCode.E_200007);
        }
    }


    @ApiOperation("门店条件列表查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeDTO", value = "门店dto", dataType = "StoreDTO", paramType = "body"),
            @ApiImplicitParam(name = "pageNo", value = "页码", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", dataType = "int", required = true, paramType = "query")
    })
    @PostMapping("/my/stores/merchants/page")
    public PageVO<StoreDTO> queryStoreByPage(@RequestBody StoreDTO storeDTO, @RequestParam Integer pageNo, @RequestParam Integer pageSize) {
        //商户id
        Long merchantId = SecurityUtil.getMerchantId();
        storeDTO.setMerchantId(merchantId);
        PageVO<StoreDTO> storeDTOS = merchantService.queryStoreByPage(storeDTO, pageNo, pageSize);
        return storeDTOS;
    }


    @ApiOperation("门店列表查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页码", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = true, dataType = "int", paramType = "query")
    })
    @PostMapping(value = "/my/stores/merchants/page")
    public PageVO<StoreDTO> queryStoreByPage(@RequestParam Integer pageNo, @RequestParam Integer pageSize) {
        //商户id
        Long merchantId = SecurityUtil.getMerchantId();
        //查询条件
        StoreDTO storeDTO = new StoreDTO();
        storeDTO.setMerchantId(merchantId);
        return merchantService.queryStoreByPage(storeDTO, pageNo, pageSize);
    }


    @ApiOperation("在某商户下新增门店,并设置管理员")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeDTO", value = "门店信息", required = true, dataType = "StoreDTO", paramType = "body"),
            @ApiImplicitParam(name = "staffIds", value = "管理员id集合", required = true, allowMultiple = true, dataType = "Long", paramType = "query")
    })
    @PostMapping(value = "/my/stores")
    public void createStore(@RequestBody StoreDTO storeDTO, @RequestParam List<Long> staffIds) {
        Long merchantId = SecurityUtil.getMerchantId();
        storeDTO.setMerchantId(merchantId);
        storeDTO.setStoreStatus(true);
        merchantService.createStore(storeDTO, staffIds);
    }


}

